import React from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { useUserStore } from '@/stores'

interface ProtectedRouteProps {
  children: React.ReactNode
  requireAdmin?: boolean
}

const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ 
  children, 
  requireAdmin = false 
}) => {
  const { user, token } = useUserStore()
  const location = useLocation()

  if (!token || !user) {
    return <Navigate to="/auth/login" state={{ from: location }} replace />
  }

  if (requireAdmin && user.role !== 'admin') {
    return <Navigate to="/403" replace />
  }

  return <>{children}</>
}

export default ProtectedRoute